package com.sgb.kjwl.view.ui.adapter.self.order;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.user.OrderServiceEntity;
import com.sgb.kjwl.view.ui.MoneyTextWatcher;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.StringUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;

/**
 * 服务维护
 */
public class OrderServiceAdapter extends BaseRecycleAdapter<OrderServiceEntity> {

    private final Context context;
    private final boolean isEdit;
    private final HashMap<Integer, String> transportPrice;
    private final HashMap<Integer, String> loadingPrice;


    public OrderServiceAdapter(Context context, List<OrderServiceEntity> data, boolean isEdit) {
        super(context, R.layout.item_order_service, data);
        this.context = context;
        this.isEdit = isEdit;
        transportPrice = new HashMap<>();
        loadingPrice = new HashMap<>();
    }

    @Override
    public void convert(ItemViewHolder holder, final OrderServiceEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        EditText etTransport = holder.itemView.findViewById(R.id.tv_transport_price);
        EditText etLoading = holder.itemView.findViewById(R.id.tv_loading_price);
        XRecyclerView listItem = holder.itemView.findViewById(R.id.list_item);

        OrderItemAdapter itemAdapter = new OrderItemAdapter(context, entity.getItemList());
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        listItem.setLayoutManager(layoutManager);
        listItem.setAdapter(itemAdapter);
        listItem.setLoadingMoreEnabled(false);
        listItem.setPullRefreshEnabled(false);

        if (!isEdit) {
            etTransport.setFocusable(false);
            etTransport.setFocusableInTouchMode(false);
            etLoading.setFocusable(false);
            etLoading.setFocusableInTouchMode(false);
        }

        if (entity.getIsTransport().equals("1")) {
            holder.itemView.findViewById(R.id.ll_transport).setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(entity.getTransportPrice())) {
                etTransport.setText(StringUtil.showPrice(entity.getTransportPrice()));
                transportPrice.put(holder.getAdapterPosition(), entity.getTransportPrice());
            }
        } else {
            holder.itemView.findViewById(R.id.ll_transport).setVisibility(View.GONE);
        }

        if (entity.getIsLoading().equals("1")) {
            holder.itemView.findViewById(R.id.ll_loading).setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(entity.getLoadingPrice())) {
                etLoading.setText(StringUtil.showPrice(entity.getLoadingPrice()));
                loadingPrice.put(holder.getAdapterPosition(), entity.getLoadingPrice());
            }
        } else {
            holder.itemView.findViewById(R.id.ll_loading).setVisibility(View.GONE);
        }

        etTransport.addTextChangedListener(new MoneyTextWatcher(etTransport) {
            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(editable)) {
                    transportPrice.remove(holder.getAdapterPosition());
                } else {
                    transportPrice.put(holder.getAdapterPosition(), StringUtil.formatUploadPrice(editable.toString()));
                }
            }
        });

        etLoading.addTextChangedListener(new MoneyTextWatcher(etLoading) {
            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(editable)) {
                    loadingPrice.remove(holder.getAdapterPosition());
                } else {
                    loadingPrice.put(holder.getAdapterPosition(), StringUtil.formatUploadPrice(editable.toString()));
                }

            }
        });
    }

    public HashMap<Integer, String> getTransportPrice() {
        return transportPrice;
    }

    public HashMap<Integer, String> getLoadingPrice() {
        return loadingPrice;
    }

}
